package com.atguigu.c_designed.m3beheviorModel.b4Iterator;

import java.util.ArrayList;
import java.util.List;

public class IteratorClient {

    public static void main(String[] args) {
        Course java = new Course("Java架构");
        Course javaBase = new Course("Java基础");
        Course design = new Course("设计模式");
        Course ai = new Course("人工智能");
        IAggregate aggregate = new ConcoreteAggeGate();
        aggregate.add(java);
        aggregate.add(javaBase);
        aggregate.add(design);
        aggregate.add(ai);
        System.out.println("===========课程列表==========");
        printCourse(aggregate);
        aggregate.remove(ai);
        System.out.println("===========删除操作之后的课程列表==========");
        printCourse(aggregate);
    }
    private static void printCourse(IAggregate aggregate) {
        Iterator<Course> i = aggregate.iterator();
        while (i.hasNext()){
            Course course = i.next();
            System.out.println("《" + course.getName()  + "》");
        }
    }
}

interface Iterator<E> {
    E next();
    boolean hasNext();
}
class ConcoreteIterator<E> implements Iterator<E> {
    private List<E> list;
    private int cursor;
    private E element;

    public ConcoreteIterator(List<E> list) {
        this.list = list;
    }

    @Override
    public E next() {
        System.out.print("当前位置 " + cursor + " : ");
        element = list.get(cursor);
        cursor ++;
        return element;
    }

    @Override
    public boolean hasNext() {
        if(cursor > list.size() - 1){
            return false;
        }
        return true;
    }
}

interface IAggregate{
    void add(Course course);
    void remove(Course course);
    Iterator<Course> iterator();
}

class ConcoreteAggeGate implements IAggregate{
    private List courseList;

    public ConcoreteAggeGate() {
        this.courseList = new ArrayList();
    }
    public void add(Course course) {
        courseList.add(course);
    }
    public void remove(Course course) {
        courseList.remove(course);
    }
    public Iterator<Course> iterator() {
        return new ConcoreteIterator<Course>(courseList);
    }
}

class Course {
    private String name;
    public Course(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}